package com.zhuss.food.processor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.food.order.wechat.CreateBoxOrderBo;
import com.zhuss.bean.bo.food.order.wechat.CreateOrderBo;
import com.zhuss.bean.bo.food.order.wechat.CreateOrderProductBo;
import com.zhuss.bean.unit.core.user.UserBean;
import com.zhuss.bean.vo.food.product.DetailProductVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.ScheduleDao;
import com.zhuss.core.processor.UserBindCheck;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.EnumUtil;
import com.zhuss.enums.IsEnum;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.OrderStatus;
import com.zhuss.enums.ProductType;
import com.zhuss.exception.WpsException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.DateUtil;
import com.zhuss.util.JsonUtil;

/**
 * 创建公务接待订单 processor
 * @Author: zhushanshuo
 * @Date: 2020/03/08 16:01
 */

@Component("boxOrderCreate")
public class BoxOrderCreate extends AbstractProcessor<Object, CreateBoxOrderBo> implements Processor<Object, CreateBoxOrderBo> {
	
	@Autowired
	@Qualifier("orderCreate")
	private Processor<Object, CreateOrderBo> orderCreate;
	
	@Autowired
	@Qualifier("productRead")
	private ProductRead productRead;
    
	@Autowired
	private ScheduleDao scheduleDao;
	
	@Autowired
	private UserBindCheck userBindCheck;
	

	@Override
	public void check(CreateBoxOrderBo bo) throws WpsException {
		Long userId = bo.getConcurrentUserId();
		Long productId = bo.getProductId();
		
		if (!userBindCheck.process(userId)) {
			throw new WpsException(ExceptionCode.用户信息过期);
		}
		
		UserBean user = bo.getConcurrentUser();
		Integer isLimitArea = user.getIsLimitArea();
		DetailProductVo product = (DetailProductVo) productRead.detail(productId);
		Integer area = bo.getArea();
		
		Integer productArea = product.getArea();
		if (!productArea.equals(area)) {
			throw new WpsException(ExceptionCode.下单区域商品不匹配);
		}
		
		Integer userArea = bo.getConcurrentUser().getArea();
		if (!userArea.equals(area) && IsEnum.是.sameKey(isLimitArea)) {
			throw new WpsException(ExceptionCode.请在所属区域下单);
		}
	}
	

	@Override
	public Object process(CreateBoxOrderBo bo) throws WpsException {
		String phone = bo.getPhone();
		Date date = bo.getDate();
		Long productId = bo.getProductId();
		Integer foodTime = bo.getFoodTime();
		Integer personNum = bo.getPersonNum();
		Integer havingStandard = bo.getHavingStandard();
		Integer drinkType = bo.getDrinkType();
		String comment = bo.getComment();
		Long userId = bo.getConcurrentUserId();
		
		Integer area = bo.getArea();
		
		DetailProductVo product = (DetailProductVo) productRead.detail(productId);
		
		// 扣减公务预定包厢的库存 ，  返回受影响数量
		date = DateUtil.toDateOnly(date);
		Integer influnce = scheduleDao.usedFoodTime(date, foodTime, 1, area);
		if (influnce.equals(0)) {
			throw new WpsException(ExceptionCode.包厢已满);
		}
		
		List<CreateOrderProductBo> createOrderProductBoList = new ArrayList<CreateOrderProductBo>();
		
		Long price = 0l;
		Long count = 1l;
		
		Long amount = price * count; // 总价
			
		CreateOrderProductBo c = new CreateOrderProductBo();
		c.setAmount(amount);
		c.setCount(count.longValue());
		c.setPrice(price);
		c.setProductName(product.getName());
		c.setProductNo(product.getProductNo());
		c.setProductId(product.getProductId());
		c.setStatus(OrderStatus.未接单.key());
		c.setConcurrentUserId(userId);
		createOrderProductBoList.add(c);
		
		CreateOrderBo orderBo = new CreateOrderBo();
		BeanUtils.copyProperties(bo, orderBo);
		orderBo.setAmount(amount);
		orderBo.setCount(count.longValue());
		orderBo.setProductCount(1l);
		orderBo.setStatus(OrderStatus.未接单.key());
		orderBo.setType(ProductType.公务接待.key());
		orderBo.setConcurrentUserId(userId);
		orderBo.setCreateOrderProductBoList(createOrderProductBoList);
		orderBo.setArea(area);
		
		// 公务接待数据：
		orderBo.setPhone(phone);
		orderBo.setDate(date);
		orderBo.setFoodTime(foodTime);
		orderBo.setPersonNum(personNum);
		orderBo.setHavingStandard(havingStandard);
		orderBo.setDrinkType(drinkType);
		orderBo.setComment(comment);
		orderCreate.process(orderBo);
		return null;
	}


	@Override
	public Object log(CreateBoxOrderBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.餐饮.key());
    	record.setAction(LogActionEnum.公务接待订单创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(EnumUtil.containsKey(ProductType.class, ProductType.公务接待.key()));
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}

}
